from lxml import etree
import requests
import pandas as pd


class hurun:

    def __init__(self):

        self.url = 'https://finance.sina.com.cn/zt_d/subject-1667873035/'
        self.headers = {
                "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
                "accept-encoding": "gzip, deflate, br, zstd",
                "accept-language": "zh-CN,zh;q=0.9",
                "cache-control": "no-cache",
                "cookie": "UOR=cn.bing.com,finance.sina.com.cn,; SINAGLOBAL=113.104.214.245_1728659706.965659; Apache=113.104.214.245_1728659706.965661; ULV=1728659716339:2:2:2:113.104.214.245_1728659706.965661:1728659706844",
                "pragma": "no-cache",
                "priority": "u=0, i",
                "referer": "https://cn.bing.com/",
                "sec-ch-ua": "\"Google Chrome\";v=\"129\", \"Not=A?Brand\";v=\"8\", \"Chromium\";v=\"129\"",
                "sec-ch-ua-mobile": "?0",
                "sec-ch-ua-platform": "\"Windows\"",
                "sec-fetch-dest": "document",
                "sec-fetch-mode": "navigate",
                "sec-fetch-site": "cross-site",
                "sec-fetch-user": "?1",
                "upgrade-insecure-requests": "1",
                "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36"
            }



    def get_html(self):
        response = requests.get(self.url, headers=self.headers)
        response.encoding = 'utf-8'
        return response.text

    # 没有动态加载 源码已有全部数据 点击按钮进行分批展示
    def parse_data(self, html):
        tree = etree.HTML(html)
        tr_lis = tree.xpath('//*[@id="datalist"]/tbody/tr')
        for tr in tr_lis[1:]:
            rank = tr.xpath('./td[1]/text()')[0]
            name = tr.xpath('./td[3]/text()')[0]
            print(rank,name)

    def use_pd(self):
        # 最快方法 但没什么意义 
        print(pd.read_html(self.url)[0])



    def run(self):
        # self.parse_data(self.get_html())
        self.use_pd()

if __name__ == '__main__':
     h = hurun()
     h.run()


